package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.BookMapper;
import com.example.demo.dao.ChapterMapper;
import com.example.demo.model.Book;
import com.example.demo.model.Chapter;
import com.example.demo.model.NameAndValue;
import com.example.demo.model.User;
import com.example.demo.service.BookFeignService;
import com.example.demo.service.HistoryService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 功能描述：
 *访问小说阅读页面
 * @Author: Zhanghf
 * @Date: 2021/5/21 15:37
 */
@Controller
public class TextController {
    @Autowired
    private BookFeignService bookFeignService;
    @Autowired(required = false)
    private HistoryService historyService;
    @Autowired(required = false)
    private BookMapper bookMapper;
    @Autowired
    private ObjectMapper objectMapper;
    @RequestMapping(value = "text")
    public String text(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        String chid = request.getParameter("chid");
        int chapterid = Integer.parseInt(chid);

        Integer bookid = Integer.parseInt(request.getParameter("bookid"));


        String title = bookFeignService.gettitle(bookid, chapterid);
        Book book = bookMapper.selectOne(new QueryWrapper<Book>().eq("id", bookid).select("name","latestnum"));
        String bookname = book.getName();
        Integer latestnum = book.getLatestnum();
        model.addAttribute("user",user);
        model.addAttribute("latestnum", latestnum);
        model.addAttribute("chaptersid", chapterid);
        model.addAttribute("bookid", bookid);
        model.addAttribute("bookname", bookname);
        model.addAttribute("title", title);

        String rolename = request.getParameter("rolename");
        if (rolename!=null)
        {
            String content = request.getParameter("content");

            String json = bookFeignService.get(content, rolename);
            HashMap<String, Integer> stringIntegerHashMap = objectMapper.readValue(json, new TypeReference<HashMap<String, Integer>>() {
            });
//            for (Map.Entry<String, Integer> stringIntegerEntry : stringIntegerHashMap.entrySet()) {
//                System.out.println(stringIntegerEntry.getKey()+stringIntegerEntry.getValue());
//            }
            ArrayList<Integer> values = new ArrayList<>();
//            有<br标签>
         //   Set<String> strings = stringIntegerHashMap.keySet();
            ArrayList<String> strings = new ArrayList<>();
            for (Map.Entry<String, Integer> stringIntegerEntry : stringIntegerHashMap.entrySet()) {
                strings.add(stringIntegerEntry.getKey().replace("<br>",""));
                 values.add(stringIntegerEntry.getValue());
                //System.out.println(stringIntegerEntry.getKey()+stringIntegerEntry.getValue());
            }
            model.addAttribute("strings",strings);
            model.addAttribute("values",values);
            return "emotion";
        }
        else {
            String text = bookFeignService.gettext(bookid, chapterid);
            model.addAttribute("text", text);
        }
        //异步方法，更新阅读记录
        historyService.AysncUpdate(user,bookid,chapterid);

        return "text";
    }

}
